package com.mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.service.IProductService;
import com.mall.vo.ProductDetailVo;
import com.mall.vo.ProductVo;
import com.mall.vo.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 黄建
 * @Date: 2020/1/25 15:15
 */
@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 获取分类下的所有商品，包括子分类
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/products")
    public ResponseVo<PageInfo<ProductVo>> products(Integer categoryId, @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                                                        @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<ProductVo> productVos = productService.list(categoryId, pageNum, pageSize);

        PageInfo<ProductVo> pageInfo = new PageInfo<>(productVos);

        return ResponseVo.success(pageInfo);
    }

    /**
     * 获取商品详细信息
     * @param productId
     * @return
     */
    @GetMapping("/products/{productId}")
    public ResponseVo<ProductDetailVo> queryProduct(@PathVariable("productId") Integer productId){
        return productService.getProductDetail(productId);
    }
}
